package com.umfwechat.util;

import javax.crypto.IllegalBlockSizeException;

/**
 * 身份证号加解密脱敏工具类
 * @author ChenFuLong
 *
 */
public class DesIDUtil {

	
	private static final String secureKey = PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties","des.secureKey", "");	//获取密钥
	
	/**
	 * 对身份证号DES加密
	 * @param ID
	 * @return 秘文
	 */
	public static String encID(String ID) {
		if(StringUtil.isNotEmpty(ID)){
			DesUtil desUtil = new DesUtil(secureKey);
			return desUtil.enc_des(ID);
		}
		return ID;
	}
	
	/**
	 * 对身份证号DES解密
	 * @param ID
	 * @return 明文
	 */
	public static String decID(String ID){
		if (StringUtil.isNotEmpty(ID)) {
			DesUtil desUtil = new DesUtil(secureKey);
			try {
				return desUtil.dec_des(ID);
			} catch (Exception e) {
				return ID;
			}
		}
		return ID;
	}
	
	/**
	 * 对明文身份证号脱敏
	 * @param ID
	 * @return 130706********0315
	 */
	public static String ID2Star(String ID){
		if (StringUtil.isNotEmpty(ID)) {
			String head = ID.substring(0, 5);
			String tail = ID.substring(ID.length() - 4);
			return head + "********" + tail;
		}
		return ID;
	}
	
	/**
	 * 对加密身份证解密并脱敏
	 * @param ID
	 * @return 130706********0315
	 */
	public static String desID2Star(String ID){
		return ID2Star(decID(ID));
	}
}
